import VueRouter from 'vue-router'
import Login from '../components/pages/Login.vue'


import Home from '../components/pages/Home.vue'
import Welcome from '../components/pages/Welcome.vue'

// 用户管理
import Users from '../components/pages/Users.vue'

// 权限管理
import Roles from '../components/pages/jurisdiction/Roles.vue'
import Rights from '../components/pages/jurisdiction/Rights.vue'

// 商品管理
import Goods from '../components/pages/commodity/Goods.vue';
import Params from '../components/pages/commodity/Params.vue';
import Categories from '../components/pages/commodity/Categories.vue'
import Add from '../components/pages/commodity/Add.vue'
import Edit from '../components/pages/commodity/Edit.vue'

// 订单管理
import Orders from '../components/pages/Orders.vue'

// 数据统计
import Reports from '../components/pages/Reports'

const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: Login
        },
        {
            path: '/home',
            component: Home,
            children: [
                {
                    path: '/home',
                    redirect: '/welcome'
                },
                {
                    name: 'welcome',
                    path: '/welcome',
                    component: Welcome
                },
                // 用户管理
                {
                    name: 'users',
                    path: '/users',
                    component: Users
                },

                // 订单管理
                {
                    name: 'orders',
                    path: '/orders',
                    component: Orders
                },

                // 权限管理
                {
                    // 权限列表
                    name: 'roles',
                    path: '/roles',
                    component: Roles
                },
                {
                    // 权限列表
                    name: 'rights',
                    path: '/rights',
                    component: Rights
                },

                // 商品管理
                {
                    // 商品列表
                    name: 'goods',
                    path: '/goods',
                    component: Goods
                },
                {
                    // 分类参数
                    name: 'params',
                    path: '/params',
                    component: Params
                },
                {
                    // 商品分类
                    name: 'categories',
                    path: '/categories',
                    component: Categories
                },
                {
                    // 商品列表中的添加参数
                    name: 'add',
                    path: '/add',
                    component: Add
                },
                {
                    // 商品编辑
                    name: 'edit',
                    path: '/edit',
                    component: Edit
                },
                // 数据统计
                {
                    name: 'reports',
                    path: '/reports',
                    component: Reports
                }
            ]
        }
        
    ]
})


// 全局路由守卫
router.beforeEach((to, from, next) => {
    // 当用户访问登录页面直接放行
    if(to.path === '/login') return next();
    // 获取sessionStorage中有没有token
    const token = sessionStorage.getItem('token');
    // 判断有没有token,如果没有token的话重定向到登录页面
    if(!token) return next('/login');
    next();
})

export default router;